<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view();
    }

    public function doLogin(Request $request)
    {
        $post = $request->post();
        $post['userpass'] = MD5($post['userpass']);
        $res = Db::name('user')
            ->field(true)
            ->where(['username'=>$post['username'], 'userpass'=>$post['userpass']])
            ->find();

        if ($res) {
            $uid = $res['id'];
            // 查权限
            $sql = "select n.mname,n.aname from QBQ_user u,QBQ_user_role ur,QBQ_role r,QBQ_role_node rn,QBQ_node n where u.id=ur.uid and r.id=ur.rid and r.id=rn.rid and n.id=rn.nid and u.id=".$uid;
            $data = Db::query($sql);
            // 存权限列表
            Session::set('admin',['nodes'=>$data,'uid'=>$uid,'username'=>$res['username']]);
//            var_dump(Session::);die;

            return redirect('admin/index.html/index');
        } else {
            $this->error('登录失败请重新登录');
        }
    }

}
